import request from '@/utils/request'
/**
 * 获取首页-文章列表
 * @param {*} channel_id 频道id
 * @returns Promise对象
 */
// 解构赋值失败情况: 字段的值为undefined的时候, 才会用=后面的默认值
export const getArticleListAPI = ({ channel_id, timestamp = new Date() }) =>
  request({
    url: '/v1_0/articles',
    method: 'GET',
    params: {
      channel_id,
      timestamp
    }
    // axios发现params的参数名对应的值为null, 会自动忽略此参数不拼接在url?后面
  })

// 首页-反馈-不喜欢文章
export const dislikeArticleAPI = (artId) => request({
  url: '/v1_0/article/dislikes',
  method: 'POST',
  data: {
    target: artId
  }
})

// 首页-反馈-举报文章
export const reportArticleAPI = ({ artId, type }) => request({
  url: '/v1_0/article/reports',
  method: 'POST',
  data: {
    target: artId,
    type: type,
    remark: '就是其他问题' // 思路: 点击其他问题, 显示一个输入框, 点击确定的时候, 再调用接口
  }
})

// 获取 - 文章详情
export const getArticleDetailAPI = ({ artId }) => request({
  url: `/v1_0/articles/${artId}`
})

// 文章 - 关注"作者"作者作者
export const followedAuthorAPI = (authorId) => request({
  url: '/v1_0/user/followings',
  method: 'POST',
  data: {
    target: authorId
  }
})

// 文章 - 取消关注"作者"
export const unFollowedAuthorAPI = (authorId) => request({
  url: `/v1_0/user/followings/${authorId}`,
  method: 'DELETE'
})

// 文章 - 点赞
export const likeArticleAPI = (artId) => request({
  url: '/v1_0/article/likings',
  method: 'POST',
  data: {
    target: artId
  }
})
// 文章 - 取消点赞
export const unLikeArticleAPI = (artId) => request({
  url: `/v1_0/article/likings/${artId}`,
  method: 'DELETE'
})

// 评论 - 获取文章评论列表
export const getCommentsListAPI = ({ artId, offset = null, limit = 10 }) => request({
  url: '/v1_0/comments',
  params: {
    type: 'a',
    source: artId,
    offset: offset, // axios遇到值为null时, 会忽略当前的参数key和值null
    limit: limit
  }
})
// 评论 - 点赞
export const likeCommentAPI = (commentId) => request({
  url: '/v1_0/comment/likings',
  method: 'POST',
  data: {
    target: commentId
  }
})

// 评论 - 取消点赞
export const unLikeCommentAPI = (commentId) => request({
  url: `/v1_0/comment/likings/${commentId}`,
  method: 'DELETE'
})

// 评论 - 发布评论
export const sendCommentAPI = ({ target, content, artId = null }) => {
  const obj = {
    target: target, // 文章ID, 或评论ID
    content: content
  }
  if (artId !== null) {
    // 如果外面传入artId, 证明是要对评论进行回复, 需要第三个参数
    // 对评论进行回复, 我才需要添加文章ID字段和值
    // 如果target是评论的ID(对评论进行回复), 才需要携带文章ID, 如果target是文章的ID(无需此参数)
    obj.art_id = artId
  }
  return request({
    url: '/v1_0/comments',
    method: 'POST',
    data: obj // axios会忽略params参数值为null的情况, data方式请求体传递null, axios是不会忽略这对参数的
  })
}
